﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Common;

namespace DAL
{
    public class BackupLog
    {   
        //Delete
        public void DelDatabaseLog(int BackupKey)
        {   
            CIMDataContext db = new CIMDataContext();

            Sys_BackupLog backup = db.Sys_BackupLog.Single(d => d.BackupKey == BackupKey);

            string filename = HttpContext.Current.Server.MapPath("../") + @"Backup\" + backup.FileName;

            WebUtility.DelUploadFile(filename);

            db.Sys_BackupLog.DeleteOnSubmit(backup);
            db.SubmitChanges();
        }

        public List<string> SelectDatabaseFilename()
        {
            CIMDataContext db = new CIMDataContext();

            return (from d in db.Sys_BackupLog
                    orderby d.BackupKey descending
                    select d.FileName ).ToList();
        }

        //条件查询
        public IQueryable<Sys_BackupLog> SelectDatabaseLog(Hashtable QueryTable)
        {
            CIMDataContext db = new CIMDataContext();

            IQueryable<Sys_BackupLog> Query;

            if (QueryTable == null)
            {
                Query = from d in db.Sys_BackupLog
                        orderby d.BackupKey descending
                        select d;
            }
            else
            {
                Query = from d in GetConditionQuery(QueryTable)
                        orderby d.BackupKey descending
                        select d;
            }
            return Query;
        }

        private IQueryable<Sys_BackupLog> GetConditionQuery(Hashtable QueryTable)
        {
            CIMDataContext db = new CIMDataContext();

            var predicate = PredicateBuilder.True<Sys_BackupLog>();

            return db.Sys_BackupLog.Where(predicate);
        }
    }

    public class RestoreLog
    {
        //Delete
        public void DelDatabaseLog(int RestoreKey)
        {
            CIMDataContext db = new CIMDataContext();

            Sys_RestoreLog restore = db.Sys_RestoreLog.Single(r => r.RestoreKey == RestoreKey);

            db.Sys_RestoreLog.DeleteOnSubmit(restore);
            db.SubmitChanges();
        }

   

        //条件查询
        public IQueryable<Sys_RestoreLog> SelectDatabaseLog(Hashtable QueryTable)
        {
            CIMDataContext db = new CIMDataContext();

            IQueryable<Sys_RestoreLog> Query;

            if (QueryTable == null)
            {
                Query = from r in db.Sys_RestoreLog
                        orderby r.RestoreKey descending
                        select r;
            }
            else
            {
                Query = from r in GetConditionQuery(QueryTable)
                        orderby  r.RestoreKey descending
                        select r;
            }
            return Query;
        }

        private IQueryable<Sys_RestoreLog> GetConditionQuery(Hashtable QueryTable)
        {
            CIMDataContext db = new CIMDataContext();

            var predicate = PredicateBuilder.True<Sys_RestoreLog>();

            return db.Sys_RestoreLog.Where(predicate);
        }
    }
}
